<template>
  <div>
    <h2>随机点名</h2>
    <div class="box">
      <span>名字是：</span>
      <div class="qs" v-html="username">{{username}}</div>
    </div>
    <div class="btns">
      <button class="start" @click="start">开始</button>
      <button class="end" @click="end">结束</button>
    </div>
    <span>{{nameArr.length}}</span>
  </div>
</template>

<script>
export default {
  data(){
    return {
      username:'这里显示姓名',
      nameArr:['赵子龙','关云长','张翼德','诸葛孔明','曹孟德','孙仲谋'],
      time:0,
      nameList:[]
    }
  },
  methods:{
    start(){
      clearInterval(this.time)
      let then = this
      this.time = setInterval(function () {
        let randomIndex = parseInt(Math.random() * then.nameArr.length)
        then.username = then.nameArr[randomIndex]
      },500)
    },
    end(){ 
      clearInterval(this.time)
    },
  }
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}

h2 {
  text-align: center;
}

.box {
  width: 600px;
  margin: 50px auto;
  display: flex;
  font-size: 25px;
  line-height: 40px;
}

.qs {
  width: 450px;
  height: 40px;
  color: red;
}

.btns {
  text-align: center;
}

.btns button {
  width: 120px;
  height: 35px;
  margin: 0 50px;
}
</style>